path = require 'path'
fs = require 'fs'
# 获取命令行参数
program = require 'commander'

HttpProxy = require('../bin/middlewares/http-proxy')

program
  .version('0.0.1')
  .option('-c, --context <context>', 'web app context')
  .option('-p, --project <path>', 'Path for project development.')
  .option('-k, --appkey <key>', 'App Key')
  .option('-o, --override <override>', 'allow project resource override server resource')
  .parse(process.argv)

program.project = program.project or "demo"
context = program.context || ''

module.exports= (app)->

  project = require('../package.json').scripts.dev.match(/-p(.*?)&/)[1].trim()
  console.log "================"
  console.log project
  app_config_file = path.resolve project, "app.js" if project
  if fs.existsSync app_config_file
    app_config = fs.readFileSync app_config_file, encoding: "utf-8"
    app_config = app_config.slice app_config.indexOf '{'
    eval("app_obj = " + app_config) #use eval instead of JSON.parse
    console.log(app_obj)
    console.log "use proxy with setting:#{JSON.stringify(app_obj.proxy)}"
    app.use HttpProxy app_obj.proxy if app_obj.proxy
